package delegate;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import manager.FileManager;
import model.DataManagerModel;

/**
 * Class       - OpenRecipeViewController
 * Description - Application window to open and view a recipe
 * Notes       - First iteration for Lunchbox GUI, keeping it simple using the MVC design concept
 * @author Ryne Okimoto
 * 
 * Inspired by Fred Swartz December 2004
 */

@SuppressWarnings("serial")

public class OpenRecipeViewController extends JFrame 
{
	// Class data
	private JTextArea txaViewer = new JTextArea();
	private JLabel lblImage;
	private JButton btnEdit     = new JButton("Edit recipe");
	private FileManager recipeFileManager;
	private DataManagerModel dataModel;
	
	/**
	 * Constructor
	 * @param recipe
	 * Notes - change parameter to a model
	 */
	public OpenRecipeViewController(DataManagerModel model, FileManager recipe)
	{
		recipeFileManager = recipe;
		dataModel = model;
		JPanel view = new JPanel();
		Image rcpImage = recipe.openImage();
		
		// For initial implementation add text to a JTextArea
		txaViewer.setText(recipe.createString());
		txaViewer.setEditable(false);
		txaViewer.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		
		if(rcpImage != null){
			lblImage = new JLabel(new ImageIcon(rcpImage.getScaledInstance(200, 200, Image.SCALE_SMOOTH)));
		}
		else{
			lblImage = new JLabel();
			lblImage.setText("Image not available");
		}
		
		// Set model
		
		
		
		// Set layout
		view.setLayout(new GridBagLayout());
		this.setResizable(false);
		
		// Set constraints
		GridBagConstraints gbcTxaViewer = new GridBagConstraints();
		gbcTxaViewer.gridx = 0;
		gbcTxaViewer.gridy = 1;
		gbcTxaViewer.insets = new Insets(20, 20, 20, 20);
		
		GridBagConstraints gbcImage = new GridBagConstraints();
		gbcImage.gridx = 1;
		gbcImage.gridy = 1;
		gbcImage.insets = new Insets(0, 0, 0, 20);
		
		GridBagConstraints gbcBtnEdit = new GridBagConstraints();
		gbcBtnEdit.gridx = 1;
		gbcBtnEdit.gridy = 2;
		gbcBtnEdit.insets = new Insets(0, 0, 20, 20);
		
		// Add components
		view.add(txaViewer, gbcTxaViewer);
		view.add(lblImage, gbcImage);
		view.add(btnEdit, gbcBtnEdit);
		
		// Set listeners
		btnEdit.addActionListener(new EditClickedListener());
		
		this.setContentPane(view);
		 
		this.setTitle(recipe.getData().getRecipeName());
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.pack();
		
	} // constructor
	
	/**
	 * Class - EditClickedListener
	 * Description - 
	 *
	 */
	private class EditClickedListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			EditViewController editView = new EditViewController(dataModel, recipeFileManager);
			OpenRecipeViewController.this.dispose();
			editView.setVisible(true);
		}
	}
	
	/**
	 * Unit tester
	 * @param args
	 */
	public static void main(String[] args){
		DataManagerModel model = new DataManagerModel();
		FileManager recipeFile = new FileManager("testRead.rcp");
		OpenRecipeViewController vc = new OpenRecipeViewController(model, recipeFile);
		vc.setVisible(true);
	} // main
} // class
